This jupyter-notebook contains basic R workflow for network visualizatin.
You can understand the basic workflow by reading this, but the RCy3's documentation is really useful and you can know much more about this.
The documentation : https://www.bioconductor.org/packages/release/bioc/html/RCy3.html
To start a new session, you should do the following step.
If you are running this notebook from a Docker container, you need to specify IP address of your machine running Cytoscape. Usually, it is the IP address of your laptop/workstation. Please edit the following line before you run this notebook!
ip_address <- '192.168.1.1'
In [ ]:
# !!!!!!!!!!!! EDIT THIS VALUE FIRST!!!!!!!!!!!!!!
ip_address <- 'IP_OF_YOUR_MACHINE'
ip_address <- '137.110.137.158'
library(RCy3)
library(igraph)
library(RJSONIO)
library(httr)
library(data.table)
# Test Connection to Cytoscape
conn <- CytoscapeConnection(host = ip_address)
print(conn)
# Start from scratch: Delete all networks
deleteAllWindows(conn)
In this example, we'll use HumanNet v1 data sets as the base network. Also, for convenience, we'll use data.table library to import data.
In [ ]:
# Read as an igraph object
humannet.url <- "http://www.functionalnet.org/humannet/HumanNet.v1.benchmark.txt"
humannet.ig <- read.graph(humannet.url)
summary(humannet.ig)
In [ ]:
cw <- CytoscapeWindow('vignette', host = ip_address , graph=g, overwrite=TRUE)
In [ ]:
# Load Data
gal.table <- read.table('sampleData/galFiltered.sif',stringsAsFactors=FALSE)
head(gal.table)
In [ ]:
# create graph class
g <- new ('graphNEL', edgemode='directed')
# Get NodesVec
gal.table.nodevec <- unique(c(gal.table[[1]], gal.table[[3]]))
# add nodes to graph
for(node in gal.table.nodevec){
g <- graph::addNode(node, g)
}
# get EdgeList
gal.table.fromvec = gal.table[[1]]
gal.table.tovec = gal.table[[3]]
for (index in 1:length(gal.table.fromvec)){
g <- graph::addEdge (gal.table.fromvec[[index]] ,gal.table.tovec[[index]], g)
}
In [ ]:
# show it in cytescape
cw <- CytoscapeWindow('vignette', host = ip_address , graph=g, overwrite=TRUE)
displayGraph (cw)
layoutNetwork (cw, layout.name='degree-circle')
In [ ]:
port.number = 1234
base.url = paste("http://", ip_address, ":", toString(port.number), "/v1/", sep="")
res <-GET(base.url)
rawToChar(res$content)
In [ ]:
network.url = paste(base.url, "networks?source=url&collection=FromR", sep="")
urls <-c(
'file:///Users/kono/git/cyrest-examples/notebooks/cookbook/sampleData/galFiltered.sif',
'file:///Users/kono/git/cyrest-examples/notebooks/cookbook/sampleData/galFiltered.gml'
)
In [ ]:
res <- POST(url=network.url, body=toJSON(urls), encode="json")
network.suid = unname(fromJSON(rawToChar(res$content)))
print(network.suid)
Then, you can get this image in cytoscape.
In this example of workflow, by executing the following steps, you can load annotation data and merge it.
Now, we use the data file 'galFiltered.sif' that is the yeast data. So, we will try to add annotation data to this.
In [ ]:
# Second, import annotation data from database(in R, we can use bioconductor as database).
# import library to access database
library(org.Sc.sgd.db)
# import Data
# DESCRIPTION data
descriptions <- select(org.Sc.sgd.db, keys=getAllNodes(cw), columns="DESCRIPTION")
df.descriptions <- data.frame(descriptions)
# GENENAME data
gene.names <- select(org.Sc.sgd.db, keys=getAllNodes(cw), columns="GENENAME")
df.gene.names <- data.frame(gene.names)
# GO data
go <- select(org.Sc.sgd.db, keys=getAllNodes(cw), columns="GO")
df.go <- data.frame(go)
# Finally, merge the above two data table and push it to Cytoscape.
# set DESCRIPTION as attribute data
nodeDataDefaults(g, attr = "DESCRIPTION") <- "undefined"
attr(nodeDataDefaults(g, attr = "DESCRIPTION"), "class") <- "STRING"
# set GENENAME as attribute data
nodeDataDefaults(g, attr = "name") <- "undefined"
attr(nodeDataDefaults(g, attr = "name"), "class") <- "STRING"
# set GO as attribute data
#nodeDataDefaults(g, attr = "go") <- "undefined"
#attr(nodeDataDefaults(g, attr = "go"), "class") <- "LIST"
# marge DESCRIPTION attribute.
for (index in 1:length(df.descriptions$DESCRIPTION)){
nodeData(g, df.descriptions$ORF[[index]], "DESCRIPTION") <- df.descriptions$DESCRIPTION[[index]]
}
# marge GENENAME attribute.
for (index in 1:length(df.gene.names$GENENAME)){
nodeData(g, df.gene.names$ORF[[index]], "name") <- df.gene.names$GENENAME[[index]]
}
# marge GO attribute.
#for (index in 1:length(df.gene.names$GENENAME)){
# nodeData(g, df.gene.names$ORF[[index]], "name") <- df.gene.names$GENENAME[[index]]
#}
# show it in cytescape
cw <- CytoscapeWindow('vignette', graph=g, overwrite=TRUE)
displayGraph (cw)
layoutNetwork (cw, layout.name='degree-circle')
In [ ]:
# add attribute:"edgeType" and the type:"STRING"
edgeDataDefaults(g, attr = "edgeType") <- "undefined"
attr(edgeDataDefaults(g, attr = "edgeType"), "class") <- "STRING"
# get EdgeList and type
gal.table.fromvec <- gal.table[,1]
gal.table.type <- gal.table[,2]
gal.table.tovec <- gal.table[,3]
# add attribute value to the network
for (index in 1:length(gal.table.fromvec)){
edgeData(g, gal.table.fromvec[[index]] ,gal.table.tovec[[index]], "edgeType") <- gal.table.type[[index]]
}
cw <- CytoscapeWindow('vignette', graph=g, overwrite=TRUE)
displayGraph (cw)
layoutNetwork (cw, layout.name='degree-circle')
First, we have to set attributes to edges. Then, we can select edges based on attributes.
selectEdges(obj, edge.names, preserve.current.selection=TRUE)
In [ ]:
# clear selection
clearSelection (cw)
In [ ]:
# selection Edges
vec = c()
for (index in 1:length(gal.table.type)){
if(gal.table.type[[index]] == 'pp'){
vec = c(vec, paste(gal.table.fromvec[[index]], '(pp)', gal.table.tovec[[index]]))
}
}
selectEdges (cw, vec)
Then, you can get the output,like the follwoing image, in cytoscape.
In [ ]:
# The code is only this!!
# TODO : this methods have some error. I can't do anything with this method.
hideSelectedEdges (cw)
In [ ]:
# get attributes data
gal.node.attrs <- read.table('sampleData/galFiltered_node.attrs',stringsAsFactors=FALSE, skip=1)
head(gal.node.attrs)
In [ ]:
g <- initNodeAttribute (graph=g, attribute.name='moleculeType', attribute.type='char', default.value='undefined')
# get EdgeAttrs
gal.node = gal.node.attrs[[1]]
gal.attrs = gal.node.attrs[[3]]
for (index in 1:length(gal.node)){
nodeData (g, gal.node, 'moleculeType') <- as.character(gal.attrs[[index]])
}
cw <- setGraph (cw, g)
displayGraph (cw)
In [ ]:
# clear selection
clearSelection (cw)
In [ ]:
vac = c()
for (index in 1:length(gal.node)){
if(gal.attrs[[index]] == '1'){
vec = c(vec, gal.node[[index]])
}
}
selectNodes(cw, vec)
Then, you can get the output,like the follwoing image, in cytoscape.
In [ ]:
# TODO : There are some error in this method. I can't do anything by this.
hideSelectedNodes(cw)
We use two method in this section. 'getLayoutNames' let us know available layoutNames and we can apply one of them by 'layoutNetwork' method.
getLayoutNames(obj)
obj : a CytoscapeConnectionClass object.
layoutNetwork(obj, layout.name= grid )
obj a CytoscapeWindowClass object. layout.name a string, one of the values returned by getLayoutNames, ’grid’ by default.
In [ ]:
# get available layout names
getLayoutNames(cw)
In [ ]:
# execute layout that you want
layoutNetwork(cw, layout.name= 'isom')
Then, you can get the output,like the follwoing image, in cytoscape.
In [ ]:
# execute layout that you want
layoutNetwork(cw, layout.name= 'kamada-kawai')
Then, you can get the output,like the follwoing image, in cytoscape.
By default, Cytoscape displays nodes as pale red circles circles, edges as blue undecorated lines, selected nodes as yellow and selected edges as red. RCy3 provides an easy way to change these defaults. More interestingly, RCy3 provides easy programmatic access to the vizmapper, by means of which the size, shape and color of nodes and edges is determined by the data attributes you have attached to those nodes and edges.
There are so many methods to do this.
At this point I do every bit of the visual style manually, but it is always the same.
That is because I do not know how to transfer a visual style from one session to the next yet.
It would be great if that was automated (I do it manually for one network and then it is automatic for the following ones that have the same exact attributes)'